#include "registerwidget.h"
#include "ui_registerwidget.h"

RegisterWidget::RegisterWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::RegisterWidget)
{
    ui->setupUi(this);
    loadStyleSheet("C:/Users/shafl/Desktop/IoTDeviceManage/src/css/img.css");
    ui->linePasswd->clear();
    ui->lineUserName->clear();
    connect(ui->bntCancel,&QPushButton::clicked,this,[this](){
        emit selectStackWidget(0);
    });
    connect(ui->bntOK,&QPushButton::clicked,this,&RegisterWidget::onRegister);

}

RegisterWidget::~RegisterWidget()
{
    delete ui;
}

void RegisterWidget::loadStyleSheet(const QString &CssPath)
{
    QFile file(CssPath);
    if(!file.open(QIODevice::ReadWrite)){
        qDebug() << "open file failed";
        return;
    }
    this->setStyleSheet(file.readAll());
}

void RegisterWidget::onRegister()
{
    UserName = ui->lineUserName->text();
    PassWord = QCryptographicHash::hash(ui->linePasswd->text().toUtf8(), QCryptographicHash::Sha256).toHex();
    UserPhone = ui->linePhone->text();
    UserEmail = ui->lineEmail->text();

    if(UserName.isEmpty()||PassWord.isEmpty()||UserPhone.isEmpty()||UserEmail.isEmpty()){
        emit sendMessage("内容不能为空");
        return;
    }
    if (!registerSqlOperator.openDatabase("C:/Users/shafl/Desktop/FrameWork/test.db")) {
        qDebug() << "Failed to open database:" << registerSqlOperator.lastError();
        emit sendMessage("数据库连接错误");
        return;
    }
    QList<QVariantMap> users = registerSqlOperator.selectData("users","name='" + UserName + "'");
    if(!users.isEmpty()){
        emit sendMessage("已存在该用户");
        ui->lineUserName->clear();
        ui->linePasswd->clear();
        ui->lineEmail->clear();
        ui->linePhone->clear();
        registerSqlOperator.closeDatabase();
        return;
    }
    users = registerSqlOperator.selectData("users","email='" + UserEmail + "'");
    if(!users.isEmpty()){
        emit sendMessage("该邮箱已注册");
        ui->lineEmail->clear();
        registerSqlOperator.closeDatabase();
        return;
    }
    users = registerSqlOperator.selectData("users","phone='" + UserPhone + "'");
    if(!users.isEmpty()){
        emit sendMessage("该手机已注册");
        ui->linePhone->clear();
        registerSqlOperator.closeDatabase();
        return;
    }
    if(registerSqlOperator.tableExists("users")){
        QVariantMap data;
        data["name"] = UserName;
        data["passwd"] = PassWord;
        data["phone"] = UserPhone;
        data["email"] = UserEmail;
        data["role"] = "user";
        if (!registerSqlOperator.insertData("users", data)) {
            qDebug() << "Failed to insert data:" << registerSqlOperator.lastError();
            emit sendMessage("注册失败");
            registerSqlOperator.closeDatabase();
            return;
        }
        LogClass *m_pLog = LogClass::getInstance();
        m_pLog->log(&registerSqlOperator,OPERATION_LOG,INFO,QString("用户：%1注册成功")
                    .arg(UserName),registerSqlOperator.getUserIdByName(UserName),-1);
        emit sendMessage("注册成功");
        emit selectStackWidget(0);
        registerSqlOperator.closeDatabase();
    }else{
        qDebug() << "Failed to open table";
        emit sendMessage("数据库表错误");
        registerSqlOperator.closeDatabase();
        return;
    }
}

